{extend name="public/original" /}
{block name="title"}商品SKU设置{/block}
{block name="css"}
<style type="text/css">
  input{width: 80px};
  td{vertical-align:none;white-space:none;}
</style>
{/block}
{block name="content"}
<div class="layui-form" lay-filter="filter-form-auth">
    <div class="layui-card-body">
      <table class="layui-table">
          <thead>
              <tr>
                  <th>商品SKU</th>
                  <th width="100px;">图片</th>
                  <th width="100px;">库存</th>
                  <th width="80px;">市场价</th>
                  <th width="80px;">售卖价</th>
              </tr>
          </thead>
          <tbody id="trnum">
            {volist name="data" id="vo"}
              <tr data-id="{$vo.id}" class="gattr">
                  <td style="white-space:normal !important;">{$vo.skus}</td>
                  <td><img src="__FILEROOT__/thumb{$vo.sku_img}" alt="" width="30px" height="30px" /></td>
                  <td><input type="text" required="required" lay-verify="number" value="{$vo.stocks}" min="0" class="sold layui-input"/></td>
                  <td><input type="text" required="required" lay-verify="price" value="{$vo.market_price}" min="0.0"class="market_price layui-input"/></td>
                  <td><input type="text" required="required" lay-verify="price" value="{$vo.price}" class="price layui-input" min="0.0" step="0.1" /></td>
              </tr>
              {/volist}
          </tbody>
      </table> 
    </div> 
     <div class="layui-form-item layui-hide">
        <input type="hidden" name="{:config('this.form_token')}" value="{:token()}" />
        <input type="button" lay-submit lay-filter="submit" id="submit" value="确认">
    </div>
</div>
{/block}
{block name="footjs"}
<script type="text/javascript">
    var num = $('#trnum .gattr').length;
    var idlist = [];
    var soldlist = [];
    var pricelist = [];

    for (var i = 0; i < $('.sold').length; i++) {
        soldlist[i] = [];
        soldlist[i] = $('.sold').eq(i).val();
    }

  layui.use(['form'], function(){
    var form = layui.form;
    var flag = true;

    form.verify({
      price: function(value, item){
        var reg = /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/;
        if(!reg.test(value)){
          return '请输入正确的价格';
        }
      }
    })
    //监听提交
    if(flag == true){
        form.on('submit(submit)', function(data){
            flag = false;
            var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引  

            var soldlist_new = [];
            var market_price = [];
            for (var i = 0; i < num; i++) {
                idlist.push($('#trnum .gattr').eq(i).attr('data-id'));
                pricelist.push($('#trnum .gattr').eq(i).find('.price').val());
                soldlist_new.push($('#trnum .gattr').eq(i).find('.sold').val());
                market_price.push($('#trnum .gattr').eq(i).find('.market_price').val());
            } 

            for (var i = 0; i < soldlist_new.length; i++) {
                if(parseInt(soldlist_new[i]) < parseInt(soldlist[i])){
                    layer.msg('添加的库存不能小于初始值');
                    return false;
                } 
            }

            $.post("{:route('Shop/addsold')}",{idlist:idlist,soldlist:soldlist_new,market_price:market_price,pricelist:pricelist}, function(d){
              flag = false;
              if(d.code == 1){
                  parent.layer.msg('修改成功');
                  parent.layer.close(index); //执行关闭 
              }else{
                  layer.msg('修改失败', {icon: 5});
                  parent.layer.close(index); //执行关闭 
              }
            });

        });
    }
    
    
  })
</script>

{/block}
